/**
 * @param {String} type 绘制类型，包括：point、line、polygon、circle、rectangle、square
 * @param {success} success 绘制完成后的回调函数，参数为绘制的feature
 */
function createDraw({
    type='Point',
    source,
    success
}) {
    let geometryFunction = null;
    let maxPoints = 0;
    if (type === 'rectangle') {
        type = 'LineString';
        maxPoints = 2;
        geometryFunction= function (coordinates, geometry) {
            if (!geometry) {
              //多边形
              geometry = new ol.geom.Polygon(null)
            }
            var start = coordinates[0]
            var end = coordinates[1]
            geometry.setCoordinates([
              [start, [start[0], end[1]], end, [end[0], start[1]], start],
            ])
            return geometry
          }
    }else if (type ==='square') {
        type= "Circle",
        source,
        geometryFunction = ol.interaction.Draw.createRegularPolygon(4)
    }
    let draw = new ol.interaction.Draw({
        type: type,
        source,
        geometryFunction: geometryFunction,
        maxPoints: maxPoints
    })
    if (success) {
        draw.on('drawend', e=> {
            success(e.feature)
        })
    }
    return draw
}

export {createDraw}